<template>
	<view class="">
	<view style=" display: flex;justify-content: center;
		background-color: #CFEAF1;height: 44px;width: 100%;
		align-items: center;position: sticky;top: 0;z-index: 1;">
			<h3>我的</h3>
		</view>
	<view style="display: flex;flex-direction: column;align-items: center;justify-content: center;">
		<image v-show="ok" class="img" src="../../static/nolog.png"></image>
		<image v-show="!ok" class="img" :src="userone.chooseImage"></image>
		
		<text v-show="ok" style="margin-top: 10px;" @click="login()">点击登录</text>
		<text v-show="!ok" style="margin-top: 10px;">{{userone.username}}</text>
		
		<view class="" style="display: flex;align-items: center; margin-top: 30px;" v-show="!ok">
			<uni-section title="个性签名:" sub-title="" type="square" style="background-color: transparent;">	
		</uni-section>
	    <em style="font-size: 20px;width: 250px;">{{userone.signature}}</em>
		</view>
		
		
		<view class="top" style="width: 80%;">
			<!-- <uni-list-item class="top" title="修改个人信息" v-show="!ok" clickable @click="updateuser(id)" showArrow></uni-list-item> -->
			<button class="top" type="primary"  v-show="!ok"  @click="updateuser(id)">修改个人信息</button>
			
			<!-- <uni-list-item class="top" title="注销账号" v-show="!ok" clickable @click="deleteuser"></uni-list-item> -->
			<button class="top" type="warn"  v-show="!ok"  @click="deleteuser">注销账号</button>
			
			<button class="top" type="default"  v-show="!ok"  @click="out">退出登录</button>
		
		
		</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				ok:true,
				id:null,
				userone:{},
				orderlist:[]
			}
		},
		methods: {
			updateuser(id){
				uni.navigateTo({
					url:'/pages/updateuser/updateuser?id='+id
				})
			},
			login(){
				uni.navigateTo({
					url: '../login/login',
				});
			},
			deleteuser(){
				uni.showModal({
					title:'注销',
					content:"您确认注销此用户吗",
					success: res => {
						if(res.confirm){
							uni.request({
								url: 'http://localhost:8484/ttwm/ttwm/deleteUser',
								method: 'POST',
								data: {"userId":this.id},
								success: res => {
									if(res.data.success){
										this.ok=true
										uni.showToast({
											title: '注销成功,请点击登录',
											icon: 'none'
										});
										uni.setStorage({
											key:"userid",
											data:null
										})
									}
								},
								fail: () => {},
								complete: () => {}
							});
							if(this.orderlist){
								for (var i = 0; i < this.orderlist.length; i++) {
								for (var j = 0; j < this.orderlist[i].orderFoodList.length; j++) {
									uni.request({
										url: 'http://localhost:8484/ttwm/ttwm/deleteOrderFood',
										method: 'POST',
										data: {"id":this.orderlist[i].orderFoodList[j].id},
										success: res => {
											if(res.data.success){	
												
											}
										},
										fail: () => {},
										complete: () => {}
									});
								}
								uni.request({
									url: 'http://localhost:8484/ttwm/ttwm/deleteOrder',
									method: 'POST',
									data: {'orderId':this.orderlist[i].orderId},
									success: res => {
										if(res.data.success){
											
										}
									},
									fail: () => {},
									complete: () => {}
								});
								}
								
							}
							
							uni.request({
								url: 'http://localhost:8484/ttwm/ttwm/deleteCommentAll',
								method: 'POST',
								data: {
									"userId":this.id
								},
								success: res => {
									if(res.data.success){
									}
								},
								fail: () => {},
								complete: () => {
								
								}
							});
							
							
						}
						}
						});
				
			},
			out(){
				this.ok=true,
				this.id=null
				uni.setStorage({
					key:"userid",
					data:null
				})
				// location.reload();
				
			}
			
		},
		onShow() {
			
			uni.getStorage({
				key:"userid",
				success:res=>{
					if(res.data != null){
					this.id=res.data.userId
					
					}
				}
			});	
			if(this.id!=null){
			uni.request({
				url: 'http://localhost:8484/ttwm/ttwm/oneUser',
				method: 'POST',
				data: {"userId":this.id},
				success: res => {
					if(res.data.success){
						this.userone=res.data.data
						// console.log(this.userone);
						this.ok=false
						
						if(this.userone.chooseImage == null){
							this.userone.chooseImage = '../../static/nolog.png'
							
						}
						if(this.userone.signature == null){
							this.userone.signature = '无'
						}
						
					}
				},
				fail: () => {
					
				},
				complete: () => {}
			});
			
			uni.request({
				url: 'http://localhost:8484/ttwm/ttwm/allOrderToFood',
				method: 'POST',
				data: {
					"userId":this.id
				},
				success: res => {	
					if(res.data.success){
						this.orderlist=res.data.list
						
						
					}
				},
				fail: () => {},
				complete: () => {}
			});
			
			}else{
				uni.showToast({
					title: '请先登录账号',
					icon: 'none'
				});
				
				
			}
			
		}
	}
</script>

<style>
	.img{
		width: 100px;
		height: 100px;
		margin-top: 50px;
		/* border: 1px solid black; */
		border-radius: 80%;
	}
	.top{
		margin-top : 30px;
	}
</style>
